﻿using System;
using System.ComponentModel;
using System.Xml;
using XamlLocalizer.Parsing;

namespace XamlLocalizer.ViewModels
{
    public class PreviewItemViewModel : INotifyPropertyChanged
    {
        private string text;
        public string Text
        {
            get { return text; }
            set
            {
                text = value;
                RaisePropertChanged("Text");
            }
        }

        private string key;
        public string Key
        {
            get { return key; }
            set
            {
                key = value;
                RaisePropertChanged("Key");
            }
        }

        private bool isChecked;
        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                RaisePropertChanged("IsChecked");
            }
        }

        private string comment;
        public string Comment
        {
            get { return comment; }
            set
            {
                comment = value;
                RaisePropertChanged("Comment");
            }
        }

        internal ParseResultNewItem ParseItem { get; set; }

        public PreviewItemViewModel(ParseResultNewItem parseItem)
        {
            ParseItem = parseItem;
            Text = ParseItem.Text;
            IsChecked = true;
            Comment = GenerateComment();
        }

        private string GenerateComment()
        {
            //TODO
            XmlNode node = ParseItem.Node;
            if (node.NodeType == XmlNodeType.Attribute)
            {
                return String.Format("{0}.{1}", ((XmlAttribute)node).OwnerElement.Name,
                    node.Name);
            }
            else if (node.NodeType == XmlNodeType.Text)
            {
                return String.Format("{0} content", node.ParentNode.Name);
            }
            return null;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
    }
}
